/*class DefaultPage
 *
 *control the landing page and
 *header and footer
 *
 *
 *since version: 0.5
 *
 */
//instance
var defaultPage;
//class
function DefaultPage()
{
    this.username = new DOMObject("username");
    this.usernameTab = new DOMObject("usernameTab");
    this.loginAction = new DOMObject("loginAction");
    this.loginTab = new DOMObject("loginTab");
    this.setting = new DOMObject("setting");
    this.headerSearchInput = new DOMObject("headerSearchInput");
    this.headerDropHint = new DOMObject("headerDropHint");
    this.headerSearchSummit = new DOMObject("headerSearchSummit");
    this.headerLogo = new DOMObject("logo");
    this.tabSearch = new DOMObject("tabSearch");
    this.searchTab = new DOMObject("searchTab");
    this.settingTab = new DOMObject("settingTab");
}
DefaultPage.prototype.show = function()
{
    this.manageStatus();
}
DefaultPage.prototype.manageStatus = function()
{
    //change userMenu depend on loginStatus
    if(userManager.checkLoginStatus())
    {
	var jsonClientInfo = userManager.getClientInfo();
	if(!jsonClientInfo)
	{
	    Show("Problem connecting Server");
	    return false;
	}
	this.username.$().innerHTML = jsonClientInfo.username;
	this.loginAction.$().innerHTML = "logout";
	this.setting.$().innerHTML = "setting";
	this.initInteraction(true);
    }
    else
    {
	this.username.$().innerHTML = "who are you?";
	this.loginAction.$().innerHTML = "login";
	this.setting.$().innerHTML = "sign up";
	this.initInteraction(false);
	loginBox.show();
    }
}
DefaultPage.prototype.initInteraction = function(isLogin)
{
    if(isLogin)
    {
	this.username.$().onclick = this.onclickTabUsername;
	this.loginAction.$().onclick = this.onclickTabLogin;
	this.setting.$().onclick = this.onclickTabSetting;
    }
    else
    {
	this.username.$().onclick = bookLib.showLoginBox;
	this.loginAction.$().onclick = bookLib.showLoginBox;
	this.setting.$().onclick = bookLib.showSignUpBox;
    }
    this.headerSearchInput.$().onchange = bookLib.showHeaderSearchHint;
    this.headerSearchSummit.$().onclick = bookLib.searchViaHeaderSearchBox;
    this.tabSearch.$().onclick = this.onclickTabSearch;
    // click logo go searchPage which is the default login welcome page
    this.headerLogo.$().onclick = bookLib.showSearchPage;
    
}
DefaultPage.prototype.showHeaderSearchHint = function()
{
    var html  = searcher.getDropHint();
    this.headerDropHint.$().innerHTML = html;
    return true;
}
DefaultPage.prototype.applyHeaderSearch = function()
{
    searcher.applySearch(this.headerSearchInput.$().value);
}

DefaultPage.prototype.onclickTabSearch = function()
{
    defaultPage.setFrontTab(defaultPage.searchTab.$());
    bookLib.showSearchPage();
}
DefaultPage.prototype.onclickTabSetting = function()
{
    defaultPage.setFrontTab(defaultPage.settingTab.$());
    bookLib.showProfilePage();
}
DefaultPage.prototype.onclickTabLogin = function()
{
    defaultPage.setFrontTab(defaultPage.loginTab.$());
    logger.logout();
}
DefaultPage.prototype.onclickTabUsername = function()
{
    defaultPage.setFrontTab(defaultPage.usernameTab.$());
    bookLib.showProfilePage();
}
DefaultPage.prototype.setFrontTab = function(nodeTab)
{
    //clear or front class style
    var nodes = document.getElementsByClassName("back");
    for(var i = 0;i<nodes.length;i++)
    {
	nodes[i].className = nodes[i].className.replace("front","");
    }
    //add front class style;
    nodeTab.className += " front";
    nodeTab.parentNode.appendChild(nodeTab);
}